module sw.name;
/**
 * 名前に関するモジュール
 */
import nazo.string, nazo.hmac_md5, sw.siteconfig, sw.client, std.base64, std.contracts;

static const string table = 
"GHIJKLMNOPQRSTUV"
"WXYZabcdefghijkl"
"mnopqrstuvwxyz./"
"0123456789ABCDEF"
"GABCDEFGHIJKLMNO"
"PQRSTUVWXYZabcde"
"fabcdefghijklmno"
"pqrstuvwxyz./012"
"GHIJKLMNOPQRSTUV"
"WXYZabcdefghijkl"
"mnopqrstuvwxyz./"
"0123456789ABCDEF"
"GABCDEFGHIJKLMNO"
"PQRSTUVWXYZabcde"
"fabcdefghijklmno"
"pqrstuvwxyz./012";

///トリップ付きの名前の取得
string getNameWithTrip(SiteConfig* cfg,string name=""){
  if(Client.authority==Client.Authority.Admin)
    name=cfg.manager~" ★";
  else{
    name=name.replace("★","☆");
    name=name.replace("◆","◇");
    string org_name=name.getBefore('#');
    char[] trip=cast(char[])name[org_name.length+1..$];
    if(trip.length==0)
      throw new Exception("トリップが空です。");

    name=org_name;
    char[] salt=(trip~"H.")[1..3];
    foreach(inout c;salt)
      c=table[c];
    ubyte[16] tmp;
    assumeUnique(trip).hmacMd5(assumeUnique(salt), tmp);
    trip=cast(char[])std.base64.encode(cast(string)tmp)[$-10..$];
    foreach(inout c;trip)
      c=table[c];
    name~=" ◆"~trip;
  }
  return name;
}
